//
//  GoodsDetailView.m
//  StandardizedProduct
//
//  Created by 宁宁 on 16/6/27.
//  Copyright © 2016年 ym. All rights reserved.
//

#import "GoodsDetailView.h"

#import "GoodsInfoView.h"//商品信息View

@implementation GoodsDetailView
{
    UIScrollView *_scrollView;
    UIPageControl *_pageControl;
    GoodsInfoView *_infoView;
}

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self setScrollView];
        [self setInfoView];
    }
    return self;
}

- (void)setScrollView{
    
    CGFloat height = WIDTH / 8 * 5;
    
    _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, height)];
    _scrollView.contentSize = CGSizeMake(WIDTH * 5, height);
    _scrollView.delegate = self;
    _scrollView.pagingEnabled = YES;//按页滚动
    _scrollView.showsHorizontalScrollIndicator = NO;
    _scrollView.showsVerticalScrollIndicator = NO;
    _scrollView.backgroundColor = [UIColor clearColor];
    [self addSubview:_scrollView];
    
    _pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(WIDTH / 2 - 50, height - 30, 100, 30)];
    _pageControl.numberOfPages = 5;
    _pageControl.pageIndicatorTintColor = [UIColor whiteColor];
    _pageControl.currentPageIndicatorTintColor = APPCOLOR;
    _pageControl.currentPage = 0;
    [self addSubview:_pageControl];
    
    for (int i = 0; i < _pageControl.numberOfPages; i++) {
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(WIDTH * i, 0, WIDTH, height)];
        imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%d.png", i + 1]];
        [_scrollView addSubview:imageView];
    }
    
    _back = [UIButton buttonWithType:UIButtonTypeCustom];
    _back.frame = CGRectMake(10, 10, 15, 20);
    [_back setBackgroundImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
    [self addSubview:_back];
}
//滚动中
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    NSInteger pageIndex = scrollView.contentOffset.x /WIDTH;
    _pageControl.currentPage = pageIndex;
    
    //显示当前图片页
    _infoView.numOfPage.text = [NSString stringWithFormat:@"%ld / 5", pageIndex + 1];
}

- (void)setInfoView{
    _infoView = [[[NSBundle mainBundle] loadNibNamed:@"GoodsInfoView" owner:nil options:nil] lastObject];
    _infoView.frame = CGRectMake(0, WIDTH / 8 * 5, WIDTH, 300);
    [self addSubview:_infoView];
    
    _infoView.goodsName.text = @"C.P.U";
    _infoView.goodsPrice.text = [NSString stringWithFormat:@"¥ 898"];
    
    //进入店铺
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(enterToMerchant:)];
    [_infoView.enterToMerchant addGestureRecognizer:tap];
    
    
}
- (void)enterToMerchant:(UIButton *)sender{
    NSLog(@"进入店铺");
}

@end
